﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;

public partial class Admin_PreviewExportAdjust : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string Id = Request.QueryString["StockOutId"].ToString();
            PreviewEA(Id);
        }
    }
    private void PreviewEA(string stockOutId)
    {
        
        StockOut stockOut = DataRepository.StockOutProvider.GetByStockOutId(stockOutId);
        DataRepository.StockOutProvider.DeepLoad(stockOut);
        TList<StockOutDetails> liststockOutDetail = DataRepository.StockOutDetailsProvider.GetByStockOutId(stockOutId);
        
        DSStockOut.StockOutDataTable dtStockOut = new DSStockOut.StockOutDataTable();
        DSStockOut.StockOutRow dtStockOutRow = dtStockOut.NewStockOutRow();
        dtStockOutRow.StockOutId = stockOutId;
        dtStockOutRow.StockOutDate = stockOut.StockOutDate;
        dtStockOutRow.TotalAmount = stockOut.TotalAmount;
        dtStockOutRow.PrepareBy = stockOut.PreparedBySource.Name ;
        dtStockOutRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtStockOutRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;

        dtStockOut.AddStockOutRow(dtStockOutRow);
        DSStockOut.StockOutDetailsDataTable dtStockOutDetails = new DSStockOut.StockOutDetailsDataTable();
        DSStockOut.StockOutDetailsRow dtStockOutDetailsRow;
        foreach (StockOutDetails StockOutDetailItem in liststockOutDetail)
        {
            DataRepository.StockOutDetailsProvider.DeepLoad(StockOutDetailItem);
            dtStockOutDetailsRow = dtStockOutDetails.NewStockOutDetailsRow();
            dtStockOutDetailsRow.DrugId = StockOutDetailItem.DrugId;
            
            DrugDispo objDrug = DataRepository.DrugDispoProvider.GetByDrugId(StockOutDetailItem.DrugId);
            dtStockOutDetailsRow.DrugName = objDrug.DrugName;
            dtStockOutDetailsRow.Unit = objDrug.Unit;
            dtStockOutDetailsRow.Quantity = StockOutDetailItem.Quantity;
            dtStockOutDetailsRow.UnitPrice = StockOutDetailItem.UnitPrice;
            DateTime dtexp;
            // //expDate=DateTime.TryParse(StockOutDetailItem.ExpDate.Value.ToString()
            // expDate = DateTime.TryParse(StockOutDetailItem.ExpDate.Value.ToString(), out dt) ? dt : (DateTime?)null;
            if (StockOutDetailItem.ExpDate != null) dtStockOutDetailsRow.ExpDate = (DateTime)StockOutDetailItem.ExpDate;
            //dtStockOutDetailsRow.ExpDate = DateTime.TryParse(StockOutDetailItem.ExpDate.Value.ToString(), out dtexp) ? dtexp : (DateTime?)null;
            dtStockOutDetailsRow.BatchLot = StockOutDetailItem.BatchLot;
            dtStockOutDetailsRow.Remark = StockOutDetailItem.Remark;
            dtStockOutDetails.AddStockOutDetailsRow(dtStockOutDetailsRow);

        }


        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSStockOut_StockOut", dtStockOut));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSStockOut_StockOutDetails", dtStockOutDetails));
        ReportViewer1.DataBind();
    }
}
